/* drivers/input/touchscreen/goodix_tool.c
 *
 * 2010 - 2012 Goodix Technology.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be a reference
 * to you, when you are integrating the GOODiX's CTP IC into your system,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Version:1.2
 *        V1.0:2012/05/01,create file.
 *        V1.2:2012/06/08,modify some warning.
 *        V1.4:2012/08/28,modified to support GT9XX
 *
 */

#include "gt9xx_ts.h"

#define DATA_LENGTH_UINT 512
#define CMD_HEAD_LENGTH (sizeof(st_cmd_head) - sizeof(u8 *))
#define GOODIX_ENTRY_NAME "goodix_tool"

#define UPDATE_FUNCTIONS

#ifdef UPDATE_FUNCTIONS
extern s32 gup_enter_update_mode(struct i2c_client *client);
extern void gup_leave_update_mode(void);
extern s32 gup_update_proc(void *dir);
#endif

extern void gtp_irq_disable(struct goodix_ts_data *);
extern void gtp_irq_enable(struct goodix_ts_data *);

#pragma pack(1)
typedef struct
{
        u8 wr;            // write read flag��0:R  1:W  2:PID 3:
        u8 flag;          // 0:no need flag/int 1: need flag  2:need int
        u8 flag_addr[2];  // flag address
        u8 flag_val;      // flag val
        u8 flag_relation; // flag_val:flag 0:not equal 1:equal 2:> 3:<
        u16 circle;       // polling cycle
        u8 times;         // plling times
        u8 retry;         // I2C retry times
        u16 delay;        // delay befor read or after write
        u16 data_len;     // data length
        u8 addr_len;      // address length
        u8 addr[2];       // address
        u8 res[3];        // reserved
        u8 *data;         // data pointer
} st_cmd_head;
#pragma pack()

st_cmd_head cmd_head;

static struct i2c_client *gt_client = NULL;
static s32 (*tool_i2c_read)(u8 *, u16);
static s32 (*tool_i2c_write)(u8 *, u16);

s32 DATA_LENGTH = 0;
s8 IC_TYPE[16] = {0};

static s32 tool_i2c_read_no_extra(u8 *buf, u16 len)
{
        s32 ret = -1;
        s32 i = 0;
        struct i2c_msg msgs[2];

        msgs[0].flags = !I2C_M_RD;
        msgs[0].addr = gt_client->addr;
        msgs[0].len = cmd_head.addr_len;
        msgs[0].buf = &buf[0];

        msgs[1].flags = I2C_M_RD;
        msgs[1].addr = gt_client->addr;
        msgs[1].len = len;
        msgs[1].buf = &buf[GTP_ADDR_LENGTH];

        for (i = 0; i < cmd_head.retry; i++)
        {
                ret = i2c_transfer(gt_client->adapter, msgs, 2);
                if (ret > 0)
                {
                        break;
                }
        }
        return ret;
}

static s32 tool_i2c_write_no_extra(u8 *buf, u16 len)
{
        s32 ret = -1;
        s32 i = 0;
        struct i2c_msg msg;

        msg.flags = !I2C_M_RD;
        msg.addr = gt_client->addr;
        msg.len = len;
        msg.buf = buf;

        for (i = 0; i < cmd_head.retry; i++)
        {
                ret = i2c_transfer(gt_client->adapter, &msg, 1);
                if (ret > 0)
                {
                        break;
                }
        }
        return ret;
}

static s32 tool_i2c_read_with_extra(u8 *buf, u16 len)
{
        s32 ret = -1;
        u8 pre[2] = {0x0f, 0xff};
        u8 end[2] = {0x80, 0x00};

        tool_i2c_write_no_extra(pre, 2);
        ret = tool_i2c_read_no_extra(buf, len);
        tool_i2c_write_no_extra(end, 2);

        return ret;
}

static s32 tool_i2c_write_with_extra(u8 *buf, u16 len)
{
        s32 ret = -1;
        u8 pre[2] = {0x0f, 0xff};
        u8 end[2] = {0x80, 0x00};

        tool_i2c_write_no_extra(pre, 2);
        ret = tool_i2c_write_no_extra(buf, len);
        tool_i2c_write_no_extra(end, 2);

        return ret;
}

static void register_i2c_func(void)
{
        if (strncmp(IC_TYPE, "GT8110", 6) && strncmp(IC_TYPE, "GT8105", 6) && strncmp(IC_TYPE, "GT801", 5) && strncmp(IC_TYPE, "GT800", 5) && strncmp(IC_TYPE, "GT801PLUS", 9) && strncmp(IC_TYPE, "GT811", 5) && strncmp(IC_TYPE, "GTxxx", 5))
        {
                tool_i2c_read = tool_i2c_read_with_extra;
                tool_i2c_write = tool_i2c_write_with_extra;
                GTP_DEBUG("I2C function: with pre and end cmd!");
        }
        else
        {
                tool_i2c_read = tool_i2c_read_no_extra;
                tool_i2c_write = tool_i2c_write_no_extra;
                GTP_INFO("I2C function: without pre and end cmd!");
        }
}

s32 init_wr_node(struct i2c_client *client)
{
        s32 i;

        gt_client = client;
        memset(&cmd_head, 0, sizeof(cmd_head));
        cmd_head.data = NULL;

        i = 5;
        while ((!cmd_head.data) && i)
        {
                cmd_head.data = kzalloc(i * DATA_LENGTH_UINT, GFP_KERNEL);
                if (NULL != cmd_head.data)
                {
                        break;
                }
                i--;
        }

        if (i)
        {
                DATA_LENGTH = i * DATA_LENGTH_UINT + GTP_ADDR_LENGTH;
                GTP_INFO("Applied memory size:%d.", DATA_LENGTH);
        }
        else
        {
                GTP_ERROR("Apply for memory failed.");
                return FAIL;
        }

        cmd_head.addr_len = 2;
        cmd_head.retry = 5;

        register_i2c_func();
        return SUCCESS;
}

#ifdef GTP_CREATE_WR_NODE
static void unregister_i2c_func(void)
{
        tool_i2c_read = NULL;
        tool_i2c_write = NULL;
        GTP_INFO("I2C function: unregister i2c transfer function!");
}
void uninit_wr_node(void)
{
        kfree(cmd_head.data);
        cmd_head.data = NULL;
        unregister_i2c_func();
        remove_proc_entry(GOODIX_ENTRY_NAME, NULL);
}
#endif
